package com.itheima.web.controller.sms;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.service.sms.RecommendSubjectService;
import com.itheima.utils.PageResult;
import com.itheima.utils.PageUtils;
import com.itheima.utils.ResultData;
import com.itheima.vo.sms.RecommendSubject;
import com.itheima.vo.sms.RecommendSubjectVO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.logging.log4j.util.Strings;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/home/recommendSubject")
public class RecommendSubjectController {
    @DubboReference
    private RecommendSubjectService recommendSubjectService;

    /**
     * 添加首页推荐专题
     * @param recommendSubjects
     * @return
     */
    @PostMapping("/create")
    public ResultData create(@RequestBody List<RecommendSubject> recommendSubjects){
        recommendSubjectService.saveBatch(recommendSubjects);
        return ResultData.ok("添加成功",null);
    }

    /**
     * 批量删除推荐
     * @param ids
     * @return
     */
    @PostMapping("/delete")
    public ResultData delete(@RequestParam List ids){
        recommendSubjectService.removeByIds(ids);
        return ResultData.ok("删除成功",null);
    }

    /**
     * 分页查询推荐
     * @param pageNum
     * @param pageSize
     * @param subjectName
     * @param recommendStatus
     * @return
     */
    @GetMapping("/list")
    public ResultData list(Integer pageNum, Integer pageSize, String subjectName, Integer recommendStatus){
        Page<RecommendSubject> pageInfo = recommendSubjectService.getPage(pageNum,pageSize,subjectName,recommendStatus);
        PageResult page = PageResult.getPage(pageInfo);
        return ResultData.ok("查询成功",page);
    }

    /**
     * 批量修改推荐状态
     * @param ids
     * @param recommendStatus
     * @return
     */
    @PostMapping("/update/recommendStatus")
    public ResultData updateRecommendStatusByIds(Integer[] ids, Integer recommendStatus){
        recommendSubjectService.updateStatus(Arrays.asList(ids),recommendStatus);
        return ResultData.ok("修改成功",null);
    }

    /**
     * 修改推荐排序
     * @param id
     * @param sort
     * @return
     */
    @PostMapping("update/sort/{id}")
    public ResultData updateSortById(Integer id, Integer sort){
        RecommendSubject recommendSubject=recommendSubjectService.getById(id);
        recommendSubject.setSort(sort);
        recommendSubjectService.updateById(recommendSubject);
        return ResultData.ok("修改成功",null);
    }
}
